import { createSlice } from '@reduxjs/toolkit';
import { CardData } from '../../types/dataType';

const initialState: CardData[] = [
  { id: 1, title: '卡片标题1', content: '卡片内容1' },
  { id: 2, title: '卡片标题2', content: '卡片内容2' },
  { id: 3, title: '卡片标题3', content: '卡片内容3' }
];
export const listSlice = createSlice({
  name: 'list',
  initialState,
  reducers: {
    add: (state, action) => {
      state.push(action.payload);
      return state;
    },
    delete: (state, action) => {
      const id = action.payload.id;
      const idx = state.findIndex((item) => item.id === id);
      state.splice(idx, 1);
      return state;
    },
    multipleDelete: (state, action) => {
      action.payload.ids.forEach((id: number) => {
        const idx = state.findIndex((item) => item.id === id);
        state.splice(idx, 1);
      });
      return state;
    },
    update: (state, action) => {
      const { id, ...values } = action.payload;
      const idx = state.findIndex((item) => item.id === id);
      state[idx] = { ...state[idx], ...values };
      return state;
    }
  }
});
